package weibo.service;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.*;

import javax.imageio.ImageIO;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.jasper.tagplugins.jstl.core.Out;

import sun.misc.BASE64Decoder;
import weibo.database.UserDB;
import weibo.model.UserMod;
import weibo.tools.saveimage;

/**
 * Servlet implementation class userupdatehead
 */
@WebServlet("/userupdatehead")
public class userupdatehead extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public userupdatehead() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * 
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		//doPost(request,response);
		//登录控制
				HttpSession session=request.getSession();
				List<UserMod> user=(List<UserMod>) session.getAttribute("user");
				if (user==null){
					response.setCharacterEncoding("utf8");
					response.setContentType("text/html;");
					response.getWriter().append("未登录，请点击 	 <a href='./userlogin'>登录</a>");
				}else{
					doPost(request,response);
				}
		
	}

	/**
	 * 更新用户头像
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//doGet(request, response);
		HttpSession session = request.getSession();
		List<UserMod> user=(List<UserMod>) session.getAttribute("user");
		if (user!=null){
			String imagebase64=request.getParameter("imagebase64");
			if (imagebase64!=null){
				//绝对路径，后期需要优化(抛弃)
				//String UserURL="D:/workspace jee/weibo/WebContent/img/";
				//相对路径
				String FileName=user.get(0).getLogin_name()+"_head.png";
				String requestURL="./image?src="+FileName;
				saveimage saveflag=new saveimage(FileName,imagebase64);
				UserDB db=new UserDB();
				boolean result=db.UpdateUserHead(user,requestURL);
				//sql语句是否生效
				if (result && saveflag.getFlag()){
					response.getWriter().append("success");
					System.out.println(user.get(0).getLogin_name()+" 用户更改了头像");
				}else{
					response.getWriter().append("error");
					System.out.println(user.get(0).getLogin_name()+" 用户设置头像失败，SQL内或保存出错");
				}
			}else{
				response.getWriter().append("error");
				System.out.println(user.get(0).getLogin_name()+" 用户设置头像失败，无效数据");
			}
		}else{
			response.getWriter().append("error");
			System.out.println("未登录用户,所以设置头像失败");
		}
	}

}
